﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LikeObmen.UI.Web.Code.BL;
using LikeObmen.UI.Web.Code.Common;
using LikeObmen.UI.Web.Code.Data;
using LikeObmen.UI.Web.Models.Home;

namespace LikeObmen.UI.Web.Controllers
{
    public class HomeController : BaseController
    {
        private LikeHelper _likeHelper;

        public HomeController()
        {
        }

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            _likeHelper = new LikeHelper(this.VkId);
        }

        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult LoadLike(long id, long countId)
        {


            _likeHelper.ChangeLikeCount(countId);
            _likeHelper.ChangeLike(id);
            return Json(_likeHelper.GetLike());
        }
        [HttpPost]
        public ActionResult LoadLikes(string url)
        {
            bool valid = false;
            
            url = url.ToLower();
            if((url.StartsWith("http://vk.com") ||
                url.StartsWith("vk.com") ||
                url.StartsWith("http://vkontakte.com") ||
                url.StartsWith("vkontakte.com"))           &&
                (url.Contains("photo") ||
                url.Contains("wall") ||
                url.Contains("video"))
                )
            {
                valid = true;
            }


            var id = _likeHelper.SetLike(url);
            return Json(new {Likes = _likeHelper.GetLikes(), Id = id, valid = valid });
        }

        [HttpGet]
        public ActionResult Info()
        {
            return View();
        }

        [HttpGet]
        public ActionResult Faq()
        {
            return View();
        }

        public ActionResult Statistics()
        {
            return View();
        }

    }
}
